package org.looop.studio.noots.analyzer;

import java.io.IOException;

import org.apache.commons.codec.language.Metaphone;
import org.apache.lucene.analysis.Token;
import org.apache.lucene.analysis.TokenFilter;
import org.apache.lucene.analysis.TokenStream;

public class MetaphoneReplacementFilter extends TokenFilter {
	public static final String METAPHONE = "METAPHONE";
	
	private Metaphone metaphoner = new Metaphone();
	
	public MetaphoneReplacementFilter(TokenStream input) {
		super(input);
	}

	@Override
	public Token next() throws IOException {
		Token t = input.next();
		if(t == null) return null;
		try{
			return new Token(metaphoner.encode(t.termText()), t.startOffset(), t.endOffset(), METAPHONE);
		}catch(Exception e){
			return t;
		}
	}

}
